package service

import (
	"gin_gorm_oj/define"
	"gin_gorm_oj/model"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"strconv"
)

// GetCategoryList
// @Tags 私有方法
// @Summary 分类列表
// @Param authorization header string true "authorization"
// @Param page query int false "page"
// @Param size query int false "size"
// @Param keyword query string false "keyword"
// @Success 200 {string} json "{"code":"200","msg","","data":""}"
// @Router /admin/categoryList [get]
func GetCategoryList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", define.DefaultPage))
	size, _ := strconv.Atoi(c.DefaultQuery("size", define.DefaultPageSize))
	keyword := c.Query("keyword")
	list := make([]*model.Category, 0)
	var count int64
	tx := model.DB.Model(&model.Category{}).Where("name like ?", "%"+keyword+"%")
	// 分页	偏移量
	err := tx.Count(&count).Offset((page - 1) * size).Limit(size).Find(&list).Error
	if err != nil {
		log.Println("GetCategoryList err:", err)
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "获取分类失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": gin.H{
			"list":  list,
			"count": count,
			"page":  page,
			"size":  size,
		},
	})
}

// CreateCategory
// @Tags 私有方法
// @Summary 分类创建
// @Param authorization header string true "authorization"
// @Param name formData string true "name"
// @Param parentId formData string false "parentId"
// @Success 200 {string} json "{"code":"200","msg","","data":""}"
// @Router /admin/createCategory [post]
func CreateCategory(c *gin.Context) {
	parentId, _ := strconv.Atoi(c.PostForm("parentId"))
	name := c.PostForm("name")
	if name == "" {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "分类名称不能为空",
		})
		return
	}
	category := &model.Category{
		Name:     name,
		ParentID: parentId,
	}
	err := model.DB.Create(category).Error
	if err != nil {
		log.Println("CreateCategory err:", err)
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "创建分类失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "分类创建成功",
		"data": category,
	})
}

// UpdateCategory
// @Tags 私有方法
// @Summary 分类更新
// @Param authorization header string true "authorization"
// @Param id formData string true "id"
// @Param name formData string true "name"
// @Param parentId formData string false "parentId"
// @Success 200 {string} json "{"code":"200","msg","","data":""}"
// @Router /admin/updateCategory [post]
func UpdateCategory(c *gin.Context) {
	id := c.PostForm("id")
	parentId, _ := strconv.Atoi(c.PostForm("parentId"))
	name := c.PostForm("name")
	if id == "" || name == "" {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "分类ID或名称不能为空",
		})
		return
	}
	category := new(model.Category)
	err := model.DB.Where("id=?", id).First(category).Error
	if err != nil {
		log.Println("UpdateCategory err:", err)
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "分类不存在",
		})
		return
	}
	category.Name = name
	category.ParentID = parentId
	err = model.DB.Where("id=?", id).Updates(category).Error
	if err != nil {
		log.Println("UpdateCategory err:", err)
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "更新分类失败",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "分类更新成功",
		"data": category,
	})
}

// DeleteCategory
// @Tags 私有方法
// @Summary 分类删除
// @Param authorization header string true "authorization"
// @Param id query string true "id"
// @Success 200 {string} json "{"code":"200","msg","","data":""}"
// @Router /admin/deleteCategory [delete]
func DeleteCategory(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))
	log.Println("收到的id参数:", id)
	if id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "分类id不能为空",
		})
		return
	}
	category := new(model.Category)
	err := model.DB.Where("id=?", id).First(category).Error
	if err != nil {
		log.Println("DeleteCategory err:", err)
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "分类不存在",
		})
		return
	}
	//检查分类下是否有题目
	var count int64
	err = model.DB.Debug().Model(&model.ProblemCategory{}).Where("category_id=?", id).Count(&count).Error
	if err != nil {
		log.Println("DeleteCategory err:", err)
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "查询分类下题目失败",
		})
		return
	}
	if count > 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "分类下有题目，不能删除",
		})
		return
	}
	err = model.DB.Debug().Delete(&model.Category{}, id).Error
	if err != nil {
		log.Println("DeleteCategory err:", err)
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "删除分类失败",
			"data": "",
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "分类删除成功",
	})
}
